using IQIGame.Onigao.Framework;
using IQIGame.Onigao.Game;
using System.Collections.Generic;

namespace IQIGame.Onigao.GamePlay
{
    public class UISkillLvPreview : UIBaseWindow
    {
        /// <summary>
        /// 静态配置
        /// </summary>
        private static UICreateConfig _createConfig = new()
        {
            prefabName = nameof(UISkillLvPreview),
            layer = EUILayer.Pop,
        };

        /// <summary>
        /// 创建UI的配置
        /// </summary>
        public override IUICreateConfig createConfig => _createConfig;

        #region Template Generate,don't modify
        protected partial class UIB_UISkillLvPreview
        {
            #region ObjectBinding Generate 
            public IQIGame.Onigao.Framework.ExButton btnClose { protected set; get; }
            public UnityEngine.UI.ExLoopVerticalScrollRect loopVerticalScrollRect { protected set; get; }
            public virtual void InitBinding(ObjectBinding __binding)
            {
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExButton>("btnClose", out var __tbv0);
                this.btnClose = __tbv0;
                __binding.TryGetVariableValue<UnityEngine.UI.ExLoopVerticalScrollRect>("loopVerticalScrollRect", out var __tbv1);
                this.loopVerticalScrollRect = __tbv1;
            }

            #endregion ObjectBinding Generate 
        }
        #endregion Template Generate,don't modify

        #region fields & properties
        private LoopScrollAdapter<UIN_SkillLvPreviewCell> loopCells = null;

        private UD_HeroSkillLvPreview uiData => _data as UD_HeroSkillLvPreview;
        private List<int> listLves = null;

        protected UIB_UISkillLvPreview ui { get; set; }
        #endregion

        protected override void BeforeInit()
        {
            ui = new UIB_UISkillLvPreview();
            ui.InitBinding(this.csObjBind);
        }

        protected override void OnInit()
        {
            this.loopCells = this.InitLoopScroll<UIN_SkillLvPreviewCell>(this.ui.loopVerticalScrollRect, this.OnSkillLvChanged);
            this.ui.btnClose.onClick.AddListener(this.Close);
        }

        protected override void OnShow()
        {

        }

        protected override void OnHide()
        {

        }

        protected override void OnDispose()
        {
            this.loopCells = null;

            if (this.listLves != null)
                ListPool<int>.Put(this.listLves);
            this.listLves = null;
        }

        protected override void OnShow(UIBaseData data)
        {
            UD_HeroSkillLvPreview uiData = (UD_HeroSkillLvPreview)data;
            if (uiData == null)
                return;
            HeroSkill heroSkill = uiData.heroSkill;
            int maxLv = heroSkill.maxLv;

            this.listLves = ListPool<int>.Get();
            for (int i = 1; i <= maxLv; i++)
            {
                var cfg = heroSkill.GetCfgSkillLevel(i);
                if (cfg == null || !cfg.IsShow) continue;
                this.listLves.Add(i);
            }
            this.loopCells.RefillCells(this.listLves.Count);
        }

        void OnSkillLvChanged(UIN_SkillLvPreviewCell item, int index)
        {
            int skillLv = this.listLves[index];
            var cfgLv = uiData.heroSkill.GetCfgSkillLevel(skillLv);
            item.Show(uiData.heroSkill.lv, cfgLv);
        }
    }
}
